package euler.helper;

public class Utils {

	public static final String pathToFiles = "/home/gmarcone/workspace_ib_mc/ProjectEuler/src/euler/";
	public static final String EOL = System.getProperty("line.separator");

	public static String join(Object[] array, String joinString) {
		StringBuilder sb = new StringBuilder();

		for (Object o : array) {
			if (sb.length() > 0) {
				sb.append(joinString);
			}
			sb.append(String.valueOf(o));
		}

		return sb.toString();
	}

	public static String toString(int[][] matrix) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				sb.append(matrix[i][j]).append(',');
			}
			sb.append(EOL);
		}

		return sb.toString();
	}

	public static String toString(long[][] matrix) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				sb.append(matrix[i][j]).append(',');
			}
			sb.append(EOL);
		}

		return sb.toString();
	}

	public static boolean contains(int[] matrix, int i) {
		for (int j = 0; j < matrix.length; j++) {
			if (i == matrix[j]) {
				return true;
			}
		}

		return false;
	}

	public static void reemplazar(int[] array, int origen, int destino) {
		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				array[i] = (array[i] == origen) ? destino : array[i];
			}
		}
	}

	public static void reverse(final int[] array) {
		final int lastIndex = array.length - 1;
		final int lastSwapIndex = array.length / 2 - 1;

		for (int i = 0; i <= lastSwapIndex; i++) {
			swap(array, i, lastIndex - i);
		}
	}

	public static void swap(final int[] arr, final int i, final int j) {
		final int tmp = arr[i];
		arr[i] = arr[j];
		arr[j] = tmp;
	}

}
